package com.qc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.BusinessReceiveDao;
import com.qc.entity.BusinessReceive;

@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository("businessReceiveDao")
public class BusinessReceiveDaoImpl implements BusinessReceiveDao{

	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public int saveBusinessReceive(BusinessReceive businessReceive) {

		String sql = "insert into t_business_receive(shop_id,user_id,coupon_id,coupon_number)values(?,?,?,?)";
		
		return jdbcTemplate.update(sql,new Object[]{businessReceive.getShopId(),businessReceive.getUserId(),businessReceive.getCouponId(),businessReceive.getCouponNumber()});
	}
	
	@Override
	public List<BusinessReceive> listeceive(long userId, long shopId,long couponId) {
		
		
		String sql = "select a.*, a.b_id id,b.* from t_business_receive a "
				+ " left join t_coupon b on b.b_id = a.coupon_id "
				+ " where 1 = 1 and a.state = 0 ";
		
		if (userId != 0) {
			sql += " and a.user_id = "+userId+"";
		}
		
		if (shopId  != 0) {
			sql += " and a.shop_id = "+shopId+"";
		}
		
		if (couponId !=0) {
			sql += " and a.coupon_id = "+couponId+"";
		}
		
		
		sql += " order by a.created_time DESC ";
		
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BusinessReceive.class));
	}

	@Override
	public List<BusinessReceive> listUserReceive(long userId,long shopId, int state,int pageNo,int pageSize) {
		
		int beginRow = (pageNo - 1)* pageSize;
		
		String sql = "select b.*,a.*,m.shop_name from t_business_receive a "
				+ " left join t_coupon b on b.b_id = a.coupon_id "
				+ " left join t_merchant m on m.shop_id = a.shop_id "
				+ "where 1 = 1 ";
		
		if (userId != 0) {
			sql += " and a.user_id = "+userId+"";
		}
		
		if (shopId  != 0) {
			sql += " and a.shop_id = "+shopId+"";
		}
		
		sql += " and a.state = "+state+"";
		
		sql += " order by a.created_time DESC ";
		sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
		
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BusinessReceive.class));
	}
	
	@Override
	public int getListReceiveCount(long shopId) {
		
		String sql = "select count(*) from t_business_receive a "
				+ " left join t_user b on b.user_id = a.user_id "
				+ " where 1 = 1 and a.shop_id = "+shopId+" ";
		
		return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List<BusinessReceive> listAllReceive(long shopId, int pageNo, int pageSize) {
		
		int beginRow = (pageNo - 1)* pageSize;

		String sql = "select a.*,b.nickname,c.title "
				+ " from t_business_receive a "
				+ " left join t_user b on b.user_id = a.user_id "
				+ " left join t_coupon c on c.b_id = a.coupon_id"
				+ " where 1 = 1 and a.shop_id = "+shopId+" and a.state = 1 ";
		
		sql += " order by a.created_time DESC ";
		sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
		
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BusinessReceive.class));
	}

	@Override
	public int updateBusinessReceiveState(BusinessReceive businessReceive) {
		
		String sql = "update t_business_receive set state = ? where b_id = ? ";
		
		return jdbcTemplate.update(sql,new Object[]{businessReceive.getState(),businessReceive.getbId()});
	}

	@Override
	public int getSerchReceive(long shopId, long userId, long couponId) {
		
		String sql = "select count(*) from  t_business_receive where shop_id = "+shopId+" and user_id = "+userId+" and coupon_id = " +couponId;
		
		return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public BusinessReceive getCouponNumber(long shopId, String couponNumber) {
		
		String sql = "select * from t_business_receive where shop_id = "+shopId;
		
		if (StringUtil.isNotEmpty(couponNumber)) {
			
			sql += " and coupon_number = " + couponNumber;
			
		}
		
		BusinessReceive businessReceive = new BusinessReceive();
		try {
			businessReceive = (BusinessReceive) jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(BusinessReceive.class));
		} catch (DataAccessException e) {
		}
		
		return businessReceive;
	}


	


}
